package com.example.liang.basamovie.utils;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;
import com.example.liang.basamovie.R;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class ImageLoaderUtils {
    static ImageLoader imageLoader;

    public static void getimage(Context context, String url, ImageView imageView) {
        imageLoader = ImageLoader.getInstance();
        //图片存放的目录
        File cachedir = StorageUtils.getOwnCacheDirectory(context, "mnt/sdcard");
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context).
                memoryCacheExtraOptions(200, 300)
                .threadPoolSize(10)
                .imageDownloader(new BaseImageDownloader(context, 5000, 5000))
                .discCache(new UnlimitedDiscCache(cachedir))//存到指定目录
                .build();
        imageLoader.init(configuration);//初始化
        DisplayImageOptions option = new DisplayImageOptions.Builder().
                showImageOnFail(R.drawable.ysf_unsupport_mime_type)
                .showImageOnLoading(R.drawable.ysf_unsupport_mime_type)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                //.displayer(new RoundedBitmapDisplayer(10))//设置图片圆角
                .build();
        imageLoader.displayImage(url, imageView, option);//执行下载
    }


    public static void setimage(Context context, String url, ImageView imageView) {
        imageLoader = ImageLoader.getInstance();
        File cacheDir = StorageUtils.getOwnCacheDirectory(context, "mnt/sdcard");
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(300, 300)
                .threadPoolSize(5)
                .discCache(new UnlimitedDiscCache(cacheDir))
                .build();
        imageLoader.init(configuration);
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .showImageForEmptyUri(R.mipmap.ic_launcher)
                .showImageOnFail(R.mipmap.ic_launcher)
                .cacheInMemory(true)
                .cacheOnDisc(true)
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
                .build();
        imageLoader.displayImage(url, imageView, options);
    }

    public static ImageLoader getImageLoader(Context context) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
                .memoryCacheExtraOptions(480, 300)
                .threadPoolSize(5)
                .build();
        imageLoader.init(configuration);
        return imageLoader;
    }

    public static Bitmap netPicToBmp(String src) {
        try {
            Log.d("FileUtil", src);
            URL url = new URL(src);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap bitmap = BitmapFactory.decodeStream(input);

            //Bitmap bitmap = Bitmap.createBitmap(myBitmap, 0, 0, width, height, matrix, true);
            return bitmap;
        } catch (IOException e) {
            // Log exception
            return null;
        }
    }
}

